package com.bupt.liutong.admin.shop.bo;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.form.UserForm;
import com.bupt.liutong.admin.shop.dao.CartDao;
import com.bupt.liutong.admin.shop.dao.OrderDao;
import com.bupt.liutong.admin.shop.dto.Cart;
import com.bupt.liutong.admin.shop.dto.Order;
import com.bupt.liutong.admin.shop.dto.OrderProd;
import com.bupt.liutong.admin.shop.form.CartForm;
import com.bupt.liutong.util.BeanUtils;

public class OrderBo extends BaseBo {
	
	private OrderDao orderDao;
	private CartDao cartDao;

	@Transactional
	public void insertOrder(UserForm userForm) {
		Cart cart = new Cart();
		cart.setUserName(userForm.getUserName());
		List<Cart> carts = cartDao.getCarts(cart);
		
		// 统计订单中商品
		List<OrderProd> orderProds = new ArrayList<OrderProd>();
		Double prodPrice = 0.0;
		for (Cart iter : carts) {
			OrderProd orderProd = new OrderProd();
			BeanUtils.copyProperties(orderProd, iter);
			orderProd.setTotalPrice(orderProd.getProdPrice() * orderProd.getProdNum());
			prodPrice += orderProd.getTotalPrice();
			orderProds.add(orderProd);
		}
		
		// 提交订单
		Order order = new Order();
		order.setUserName(userForm.getUserName());
		order.setProdPrice(prodPrice);
		order.setShipFee(0.0);
		order.setTotalPrice(order.getProdPrice() + order.getShipFee());
		order.setConsignee(userForm.getRealName());
		order.setMobilePhone(userForm.getMobilePhone());
		order.setDes("身份证号：" + userForm.getIdCard());
		Integer orderId = orderDao.insertOrder(order);
		
		// 提交订单中商品信息
		for (OrderProd iter : orderProds) {
			iter.setOrderId(orderId);
			orderDao.insertOrderProd(iter);
		}
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

}
